package com.zonekey.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class JsonUtil {

	private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);

	public static <T> T jsonToObject(Object json, Class<T> clazz) {
		if (json instanceof HttpServletRequest) {
			HttpServletRequest req = (HttpServletRequest) json;
			try {
				String str = new BufferedReader(new InputStreamReader(req.getInputStream(), "utf-8")).readLine();
				return JSON.parseObject(str, clazz);
			} catch (Exception e) {
				return null;
			}
		}
		return JSON.parseObject(String.valueOf(json), clazz);
	}

}
